package com.xiaomage.handlerdemo

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.os.Message
import android.util.Log
import kotlinx.android.synthetic.main.activity_down_picture.*
import java.io.BufferedInputStream
import java.net.HttpURLConnection
import java.net.URL

class DownPictureActivity : AppCompatActivity() {
    /**
     * 图片地址集合
     */
    private val urlList = arrayOf(
            "http://img0.imgtn.bdimg.com/it/u=1597254274,1405139366&fm=23&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=3901634069,2243065451&fm=23&gp=0.jpg",
            "http://img4.imgtn.bdimg.com/it/u=1800624712,2677106110&fm=23&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=2456066925,446683653&fm=23&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=565155430,1247415230&fm=23&gp=0.jpg",
            "http://img4.imgtn.bdimg.com/it/u=2845715753,1348257911&fm=23&gp=0.jpg",
            "http://img3.imgtn.bdimg.com/it/u=3634032659,2514353810&fm=23&gp=0.jpg")
    //主线程处理handler
    private var mainHandler:Handler?=null
    private var handlerThread: HandlerThread?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_down_picture)
        mainHandler= object:Handler(){
            override fun handleMessage(msg: Message) {
                val imagebean=msg.obj as ImageBean
                Log.e("下载链接",imagebean.url)
                iv_img.setImageBitmap(imagebean.bitmap)
            }
        }
        bt_thread_down.setOnClickListener{
            //thread下载图片
            Thread(Runnable {
                for(url in urlList){
                    Thread.sleep(1000)
                    val bitmap=downPictureByUrl(url)
                    val imageBean=ImageBean(url,bitmap)
                    val msg=Message()
                    msg.obj=imageBean
                    mainHandler?.sendMessage(msg)
                }
            }).start()
        }
        bt_handler_thread_down.setOnClickListener{
            handlerThread= HandlerThread("下载图片")
            handlerThread?.start()
            val handler=Handler(handlerThread?.looper,object:Handler.Callback{
                override fun handleMessage(msg: Message): Boolean {
                    val url=msg.obj as String
                    val bitmap=downPictureByUrl(url)
                    val imageBean=ImageBean(url,bitmap)
                    val msg=Message()
                    msg.obj=imageBean
                    mainHandler?.sendMessage(msg)
                    return false
                }
            })
            var i=1L
            for(url in urlList){
                val message=Message()
                message.obj=url
                handler.sendMessageDelayed(message,i*1000)
                i += 1
            }
        }
    }

    private fun downPictureByUrl(urlStr:String):Bitmap{
        val url=URL(urlStr)
        val connection =url.openConnection() as HttpURLConnection
        val inputStream=BufferedInputStream(connection.inputStream,8*1024)
        val bitmap=BitmapFactory.decodeStream(inputStream)
        inputStream.close()
        connection.disconnect()
        return bitmap
    }
}
